//
//  redPacketResultViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/9/23.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "redPacketResultViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "CMessageMgr.h"
#import "CMessageModel.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "CHChatRoomBase.h"
#import "RedPacketInfo.h"
#import "redPacketInfoViewController.h"
@interface redPacketResultViewController ()
@property(nonatomic,strong)CContactModel *currentUser;
@property(nonatomic,strong)CContactModel *fromUser;
@property(nonatomic,weak)UIView *topView;
@end

@implementation redPacketResultViewController
-(void)setRedinfo:(RedPacketInfo *)redinfo
{
    _redinfo = redinfo;
}
-(void)setMessage:(CMessageModel *)message
{
    _message = message;
    if (self.message.GroupId.length>0) {
        self.fromUser = [[[CContactMgr alloc]init] loadContactWithUserId:_message.MineUserId];
    }
    else
    {
        self.fromUser = [[[CContactMgr alloc]init] loadContactWithUserId:_message.FriendUserId];
    }
}
-(CContactModel *)currentUser
{
    if (_currentUser == nil) {
        _currentUser = [[[CContactMgr alloc]init] loadCurContact];
    }
    return _currentUser;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(240, 240, 240);
    [self.navigationItem setNewTitleCenter:@"红包"];
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(246, 75, 77)];
    }
    else
    {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(246, 75, 77)];
    }
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(back) titleFont:Font18 titleColor:WhiteColor];

}
-(void)back
{
    for (id controller  in self.navigationController.childViewControllers) {
        if ([controller isKindOfClass:[CHChatRoomBase class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isTimeOut) {
        [self configTimeOut];
    }
    else {
    [self configTopView];
    } 
    
}
-(void)configTimeOut
{
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noredpacket"]];
    [self.view addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    UILabel *noticeLable = [[UILabel alloc]init];
    noticeLable.backgroundColor = [UIColor clearColor];
    noticeLable.textAlignment = NSTextAlignmentCenter;
    noticeLable.textColor = [UIColor whiteColor];
    noticeLable.text = @"红包已过期，无法领取";
    if ([self.fromUser.UserName isEqualToString:self.currentUser.UserName]) {
        noticeLable.text = @"红包已过期，余额已退回";
    }
    [imageview addSubview:noticeLable];
    [noticeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
}
-(void)configTopView
{
    UIView *topView = [[UIView alloc]init];
    self.topView = topView;
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(300);
    }];
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.font = [UIFont systemFontOfSize:13];
    timeLabel.textColor = RGBCOLORWITH16COLOR(0xaaaaaa);
    timeLabel.textAlignment = NSTextAlignmentLeft;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_message.TimeStamp doubleValue] / 1000.0];
    NSString *str1 = [UIUtil getOverdueYMDHMSDateStringWithTimeStamp:date];
    NSString *str2 = [UIUtil timeForMessageStyle:date];
    timeLabel.text = [NSString stringWithFormat:@"%@ %@",str1,str2];
    [topView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
        make.left.mas_equalTo(5);
        make.height.mas_equalTo(30);
    }];
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:self.fromUser.UserID]] placeholderImage:[UIImage imageNamed:@"testHeader"]];
    imageView.layer.cornerRadius = 40;
    imageView.layer.masksToBounds = YES;
    [topView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(10);
        make.width.height.mas_equalTo(80);
        make.centerX.mas_equalTo(topView);
    }];
    UILabel *nameLable = [[UILabel alloc]init];
    nameLable.textColor = RGBCOLORWITH16COLOR(0x323232);
    nameLable.text = self.fromUser.UserName;
    nameLable.font = [UIFont systemFontOfSize:15];
    nameLable.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    UILabel *contentLable = [[UILabel alloc]init];
    contentLable.font = [UIFont systemFontOfSize:20];
    contentLable.textAlignment = NSTextAlignmentCenter;
    contentLable.text = @"送你一个红包";
    contentLable.textColor = RGBCOLORWITH16COLOR(0xaaaaaa);
    [topView addSubview:contentLable];
    [contentLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLable.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    UILabel *moneyLabel = [[UILabel alloc]init];
    moneyLabel.font = [UIFont systemFontOfSize:30];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.text = [NSString stringWithFormat:@"%@%.2f",@"¥",self.redinfo.money];
    moneyLabel.textColor = RGBCOLORWITH16COLOR(0x323232);
    if (self.redinfo.money == 0) {
       moneyLabel.text = @"红包已拆完";
        moneyLabel.textColor = RGBCOLORWITH16COLOR(0xaaaaaa);

    }
      [topView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLable.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    if (self.message.GroupId.length>0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:@"看看别人的手气" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(getOtherUserMoney) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(topView.mas_bottom).offset(20);
            make.height.mas_equalTo(50);
        }];
    }
}
-(void)getOtherUserMoney
{
    __weak typeof (self) weakSelf = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[CXmppRequestMgr shareInstance]getredPacketInfoWithMessageId:self.message.MessageId successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        [hud hide:YES];
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            redPacketInfoViewController *result = [[redPacketInfoViewController alloc]init];
            result.message = self.message;
            result.money =  [responseObject[@"redPacket"][@"amount"] doubleValue];
            NSArray *array = responseObject[@"redPacketInfo"];
            NSMutableArray *infoArray = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                RedPacketInfo *info = [[RedPacketInfo alloc]init];
                info.userId = dic[@"userId"];
                info.money = [dic[@"amount"] doubleValue];
                info.time = dic[@"sendTime"];
                info.type = PERSON;
                info.redPacketId = dic[@"redPacketId"];
                info.userName = dic[@"userName"];
                info.robed = [dic[@"robStatus"] boolValue];
                [infoArray addObject:info];
            }
            result.infoArray = [infoArray copy];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.navigationController pushViewController:result animated:YES];
            });
        }
        else
        {
            [MBProgressHUD showError:@"服务器异常"];
        }
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];
        [MBProgressHUD showError:@"网络错误"];
    }];
}
@end
